package com.cx.service.impl;/**
 * @package com.cx.service.impl
 * @author flysand
 * @date 2021-11-19 22:55
 * @des CommonServiceImpl:
 */

import com.cx.common.util.CamelCaseUtils;
import com.cx.common.util.StringUtils;
import com.cx.dal.dao.*;
import com.cx.meta.enums.ServiceName;
import com.cx.meta.model.po.MusicQueryParams;
import com.cx.meta.model.po.QueryParams;
import com.cx.meta.model.po.UserLoginRecordQueryParams;
import com.cx.meta.model.po.UserQueryParams;
import com.cx.meta.model.vo.PageData;
import com.cx.service.CommonService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Location:com.cx.service.impl
 *
 * @author flysand 2021年11月19 下午 10:55
 * Description:
 **/
@Service
public class CommonServiceImpl implements CommonService {

    @Resource
    private CxUserMapper userMapper;
    @Resource
    private CxMusicMapper musicMapper;
    @Resource
    private CxUserLogMapper userLogMapper;
    @Resource
    private CxUserLoginRecordMapper userLoginRecordMapper;
    @Resource
    private CxRoleMapper roleMapper;
    @Resource
    private CxUserRoleLogMapper userRoleLogMapper;


    @Override
    public PageData<?> getByQueryParams(QueryParams queryParams, ServiceName serviceName) {
        // 分页查询
        int page = queryParams.getPage() == 0 ? 1 : queryParams.getPage();
        int rows = queryParams.getRows() == 0 ? 10 : queryParams.getRows();
        String orderBy = "";
        String sort = queryParams.getSort();
        if (StringUtils.isNotBlank(sort)) {
            sort = CamelCaseUtils.humpToLine(sort);
            orderBy = sort + " desc";
        }
        if (StringUtils.isNotBlank(queryParams.getOrder())) {
            orderBy = orderBy.replace("desc", queryParams.getOrder());
        }

        if (StringUtils.isBlank(orderBy)) {
            PageHelper.startPage(page, rows);
        } else {
            PageHelper.startPage(page, rows, orderBy);
        }
        Page<?> pageInfo = new Page<>();

        switch (serviceName) {
            case USER:
                pageInfo = userMapper.getByParams((UserQueryParams) queryParams);
            case MUSIC:
                pageInfo = musicMapper.getByParams((MusicQueryParams) queryParams);
            case USER_LOG:

            case USER_LOGIN_RECORD:
                pageInfo = userLoginRecordMapper.getByParams((UserLoginRecordQueryParams) queryParams);

        }
        return new PageData((int) pageInfo.getTotal(), pageInfo.getResult());
    }

}
